package com.fan.shop.services;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.transaction.annotation.Transactional;

import com.fan.shop.dao.CategoryDao;
import com.fan.shop.dao.ItemDao;
import com.fan.shop.dao.LocationDao;
import com.fan.shop.domain.Item;
import com.fan.shop.global.ShopGlobalUtils;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.model.userclass.ItemUserClass;

@Named("itemService")
@Transactional(readOnly = true)
public class ItemServiceImpl implements ItemService {

	@Inject
	@Named("itemDao")
	private ItemDao itemDao;

	@Inject
	@Named("locationDao")
	private LocationDao locationDao;
	
	@Inject
	@Named("categoryDao")
	private CategoryDao categoryDao;
	
	public Item getItem(String itemcode) {
		Item item = itemDao.getItem(itemcode);
		return item;
	}

	public List<Item> getAllItems() {
		List<Item> list = itemDao.getAllItems();
		return list;
	}

	public int getSize() {
		return itemDao.getSize();
	}

	@Transactional(readOnly = false)
	public void addItem(Item item) {
		itemDao.addItem(item);
	}

	@Transactional(readOnly = false)
	public void updateItem(Item item) {
		itemDao.updateItem(item);
	}

	@Transactional(readOnly = false)
	public void deleteItem(Item item) {
		itemDao.deleteItem(item);
	}

	public boolean hasItem(String itemcode) {
		return itemDao.hasItem(itemcode);
	}

	@SuppressWarnings("rawtypes")
	public List getList() {
		return getAllItems();
	}

	public List<String> getColumnNames() {
		
		
		return itemDao.getColumnNames();
	}

	public int getColumnsSize() {
		return getColumnNames().size();
	}

	public ItemDao getItemDao() {
		return itemDao;
	}

	public void setItemDao(ItemDao itemDao) {
		this.itemDao = itemDao;
	}

	public LocationDao getLocationDao() {
		return locationDao;
	}

	public void setLocationDao(LocationDao locationDao) {
		this.locationDao = locationDao;
	}

	public CategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(CategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public ItemUserClass copy(Object obj) {
		Item item = (Item)obj;
		ItemUserClass itemUserClass = new ItemUserClass();
		itemUserClass.setItemCode(item.getItemCode());
		itemUserClass.setItemName(item.getItemName());
		itemUserClass.setItemDescription(item.getItemDescription());
		itemUserClass.setCategory(categoryDao.getCategory(item.getCategory()).getName());
		itemUserClass.setOrigLocation(locationDao.getLocation(item.getOrigLocation()).toString());
		itemUserClass.setBrand(item.getBrand());
		itemUserClass.setPrice(item.getPrice());
		itemUserClass.setIcon("<img class='thumbnail' src='/" + ShopGlobalVariables.MODULE_NAME + item.getIcon() + "' height='42' width='42' />");
		//itemUserClass.setIcon(item.getIcon());
		itemUserClass.setAttributes(item.getAttributes());
		itemUserClass.setRegisteredTime(ShopGlobalUtils.getDateTimeString(item.getRegisteredTime()));
		itemUserClass.setSuspended(item.getSuspended());
		
		return itemUserClass;
	}

}
